package com.example.viewtest.AdapterGroup;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.viewtest.Dao.Bean;
import com.example.viewtest.R;

import java.util.List;

public class Adapter_FragmentTwo extends BaseAdapter {
    //定义两个类别标志
    private static final int TYPE_ONE = 0;
    private static final int TYPE_TWO = 1;
    private Context mContext;
    private List<Bean> mData;

    public Adapter_FragmentTwo(Context context, List<Bean> list) {
        mContext = context;
        mData = list;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return TYPE_ONE;
        } else {
            return TYPE_TWO;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        ViewHolder1 holder1 = null;
        ViewHolder2 holder2 = null;
        if (convertView == null) {
            switch (type) {
                case TYPE_ONE:
                    holder1 = new ViewHolder1();
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_one_view_two, parent, false);
                    holder1.vh1_image = convertView.findViewById(R.id.fragmentOne_Two_item_image);
                    convertView.setTag(R.id.fragmentTwo_One_TAG, holder1);
                    break;
                case TYPE_TWO:
                    holder2 = new ViewHolder2();
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_one_view_one, parent, false);
                    holder2.vh2_image1 = convertView.findViewById(R.id.fragmentOne_One_item_image1);
                    holder2.vh2_image2 = convertView.findViewById(R.id.fragmentOne_One_item_image2);
                    convertView.setTag(R.id.fragmentTwo_Two_TAG, holder2);
                    break;
                default:
            }
        } else {
            switch (type) {
                case TYPE_ONE:
                    holder1 = (ViewHolder1) convertView.getTag(R.id.fragmentTwo_One_TAG);
                    break;
                case TYPE_TWO:
                    holder2 = (ViewHolder2) convertView.getTag(R.id.fragmentTwo_Two_TAG);
                    break;
                default:
            }
        }

        //设置控件值
//        Bean obj = mData.get(position);
        switch (type) {
            case TYPE_ONE:
                if (mData.get(position) != null) {
                    Glide.with(mContext)
                            .load(mData.get(position).getImageId())
                            .into(holder1.vh1_image);
                }
                break;
            case TYPE_TWO:
                if (mData.get(position) != null) {
                    Glide.with(mContext)
                            .load(mData.get(position).getImageId())
                            .into(holder2.vh2_image1);
                    Glide.with(mContext)
                            .load(mData.get(position).getImageId())
                            .into(holder2.vh2_image2);
                }
                break;
            default:
        }

        return convertView;
    }

    //两个不同的ViewHolder
    private static class ViewHolder1 {
        ImageView vh1_image;
        TextView txt_aname;
    }

    private static class ViewHolder2 {
        ImageView vh2_image1;
        ImageView vh2_image2;
    }
}
